import { ObjectId } from 'mongodb'
import * as db from '../db'
import { ICompany } from '../types'
import { stats } from '../stats'
import { tokens, userInfo } from './user.service'

// 首页 热门企业
export async function list() {
  const company = await db.companyCollection
    .aggregate([
      {
        $match: {}
      },
      {
        $lookup: {
          from: 'recruit',
          localField: '_id',
          foreignField: 'companyId',
          as: 'recruits'
        }
      }
    ])
    .toArray()
  return company
}

// 查询同一创始人的公司列表
export async function companylist(token: string) {
  const id = tokens.get(token)
  console.log(id)
  const user = await db.userCollection.findOne({
    _id: new ObjectId(id)
  })
  if (user === undefined) throw stats.ERR_NOT_FOUND
  if (user.role === 1) {
    return db.companyCollection.find().toArray()
  } else {
    const company = await db.companyCollection
      .find({
        userId: new ObjectId(id)
      })
      .toArray()
    if (company === undefined) return null
    return company
  }
}

// 查询公司信息
export async function findOne(_id: string) {
  const result = await db.companyCollection.findOne({
    _id: new ObjectId(_id)
  })
  if (result === null) throw stats.ERR_NOT_FOUND
  return result
}

// 根据token查询公司信息
export async function findOneCompany(token: string) {
  const user = await userInfo(token)
  const result = await db.companyCollection.findOne({
    _id: user.companyId
  })
  if (result === null) throw stats.ERR_NOT_FOUND
  return result
}

// 根据公司Id查询职位列表
export function listRecruit(companyId: string) {
  return db.recruitCollection
    .find({
      companyId: new ObjectId(companyId)
    })
    .toArray()
}

// 添加公司
export async function add(value: ICompany, token: string) {
  const id = tokens.get(token)
  const result = await db.companyCollection.findOne({
    companyShortName: value.companyShortName
  })
  if (result !== undefined) throw stats.ERR_EXISTS
  const res = await db.companyCollection.insertOne({
    userId: id,
    ...value
  })
  return res.insertedId
}

// 编辑公司
export async function update(_id: string, params: ICompany) {
  const result = await db.companyCollection.findOne({
    _id: new ObjectId(_id)
  })
  if (result === undefined) throw stats.ERR_NOT_FOUND
  await db.companyCollection.findOneAndUpdate(
    {
      _id: new ObjectId(_id)
    },
    {
      $set: params
    }
  )
}

// 删除公司
export async function remove(_id: string) {
  const res = await db.companyCollection.findOne({
    _id: new ObjectId(_id)
  })
  if (res === undefined || res.isDisabled === 2) throw stats.ERR_NOT_FOUND
  const result = await db.companyCollection.findOneAndUpdate(
    {
      _id: new ObjectId(_id)
    },
    {
      $set: {
        isDisabled: 2
      }
    }
  )
  return result.value
}

export async function status(id: string) {
  const user = await db.companyCollection.findOne({
    _id: new ObjectId(id)
  })

  await db.companyCollection.findOneAndUpdate(
    {
      _id: new ObjectId(id)
    },
    {
      $set: {
        isDisabled: user.isDisabled === 1 ? 2 : 1
      }
    }
  )
}
